import { CtgyState, SecondCtgy, initCtgyState } from "./state"
import ctgyAPI from '@/api/ctgyApi'
import { defineStore } from 'pinia'
import { FirstCtgy, ThirdCtgy } from "../state"
import goodStorage from 'good-storage'
import { hasProps, hasArrProps } from '@/utils/objectUtil'


export default defineStore('ctgyStore', {
  state: () => {
    return initCtgyState
  },
  getters: {
    // 获取 state 中 firstCtgyList 一级分类
    getFirstCtgyList(state: CtgyState) {
      return state.firstCtgyList
    },
    // 获取 state 中 secondCtgyList 二级分类+三级分类
    getSecThirdCtgyList(state: CtgyState) {
      return state.secondCtgyList
    },
    // 获取跳转路由时保存的3级分类 (搜索使用)
    getThirdCtgy(state): ThirdCtgy {
      return hasProps(state.thirdCtgy) ? state.thirdCtgy : goodStorage.get('thirdCtgy')
    },
    // 获取跳转路由时保存的1级分类 (面包屑使用)
    getFirstCtgy(state): FirstCtgy {
      return hasProps(state.firstCtgy) ? state.firstCtgy : goodStorage.get('firstCtgy')
    },
    // 获取跳转路由时保存的2级分类 (面包屑使用)
    getSecondCtgy(state): SecondCtgy {
      return hasProps(state.secondCtgy) ? state.secondCtgy : goodStorage.get('secondCtgy')
    },
    // 获取跳转路由后列表使用的全部3级分类
    getThirdCtgyList(state): ThirdCtgy[] {
      return hasArrProps(state.thirdCtgyList) ? state.thirdCtgyList : goodStorage.get('thirdCtgyList')
    },
    // 获取跳转路由后列表使用的部分3级分类
    getSubThirdCtgyList(state): ThirdCtgy[] {
      return hasArrProps(state.subthirdCtgyList) ? state.subthirdCtgyList : goodStorage.get('subThirdCtgyList')
    },
    // 获取跳转路由后3级分类列表是否展开 (列表使用)
    getIsReadyOpen(state) {
      return state.isReadyOpen // true代表没有展开3级分类列表 false代表展开了3级分类列表
    }
  },
  actions: {
    // 保存跳转路由时3级分类(搜索框使用)
    storeThirdCtgy(thirdctgys: ThirdCtgy) {
      goodStorage.set('thirdCtgy', thirdctgys) // 将保存的1级分类缓存到 Local Storage 中
      this.thirdCtgy = thirdctgys
    },

    // 保存跳转路由时1级分类(面包屑使用)
    storeFirstCtgy(firstCtgy: FirstCtgy) {
      goodStorage.set('firstCtgy', firstCtgy) // 将保存的1级分类缓存到 Local Storage 中
      this.firstCtgy = firstCtgy
    },

    // 保存跳转路由时2级分类(面包屑使用)
    storeSecondCtgy(secondCtgy: SecondCtgy) {
      goodStorage.set('secondCtgy', secondCtgy) // 将保存的2级分类缓存到 Local Storage 中
      this.secondCtgy = secondCtgy
    },
    
    // 保存跳转路由时选中的3级分类所有列表 (列表使用)
    storeThirdCtgyList(thirdCtgyList: ThirdCtgy[]) {
      goodStorage.set('thirdCtgyList', thirdCtgyList)
      this.thirdCtgyList = thirdCtgyList
    },

    // 保存跳转路由时选中的3级分类部分列表 (列表使用)
    storeSubThirdCtgyList(subThirdCtgyList: ThirdCtgy[]) {
      goodStorage.set('subThirdCtgyList', subThirdCtgyList)
      this.subthirdCtgyList = subThirdCtgyList
    },

    // 保存跳转路由时是否需要展开所有的3级分类列表 (列表使用)
    storeIsReadyOpen(isReadyOpen: boolean) {
      this.isReadyOpen = isReadyOpen
    },

    // 将所有1级分类保存到 state 中
    async findFirstCtgyList() {
      const result = await ctgyAPI.getFirstCtgyList()
      this.firstCtgyList = result.data
    },

    // 根据1级分类的id值将所有2级分类(内包含3级分类)保存到 state 中
    async findSecThirdCtgyList(firstCtgyId: number) {
      let result = await ctgyAPI.getSecThrdCtgyList(firstCtgyId)
      result.data = result.data.map((secondctgy: SecondCtgy) => {
        secondctgy.isReadyOpen = true // 可以点击展开按钮
        // 扩展 subThirdCtgys 字段里面最多保存5项3级分类
        return (secondctgy.subThirdCtgys = secondctgy.thirdctgys.slice(0, 5), secondctgy)
      })
      // 给2级分类+3级分类赋值
      this.secondCtgyList = result.data
    }
  }
})
